﻿using Microsoft.Win32;
using MiniExcelLibs;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace IntoLog
{
    public class LogViewModel : BindableBase
    {
        private DateTime dateFrom;
        public DateTime DateFrom
        {
            get => dateFrom; set => SetProperty(ref dateFrom, value);
        }

        private DateTime dateTo;
        public DateTime DateTo
        {
            get => dateTo; set => SetProperty(ref dateTo, value);
        }

        public ObservableCollection<LogMsg> QueryMsg { get; set; } = new ObservableCollection<LogMsg>();

        private DelegateCommand queryTodayCmd;
        /// <summary>
        /// 查询今日
        /// </summary>
        public DelegateCommand QueryTodayCmd => queryTodayCmd ?? (queryTodayCmd = new DelegateCommand(() =>
        {
            QueryMsg.Clear();
            using (LogDbContext db = new LogDbContext())
            {
                var query = db.Logs.Where(o => o.LogTime > DateTime.Today).ToList();
                if (query != null)
                {
                    foreach (var log in query)
                    {
                        QueryMsg.Add(log);
                    }
                }
            }
        }));

        private DelegateCommand queryFromToCmd;
        /// <summary>
        /// 查询区间
        /// </summary>
        public DelegateCommand QueryFromToCmd => queryFromToCmd ?? (queryFromToCmd = new DelegateCommand(() =>
        {
            QueryMsg.Clear();
            using (LogDbContext db = new LogDbContext())
            {
                var query = db.Logs.Where(o => o.LogTime >= dateFrom && o.LogTime <= dateTo).ToList();
                if (query != null)
                {
                    foreach (var log in query)
                    {
                        QueryMsg.Add(log);
                    }
                }
            }
        }));

        private DelegateCommand exportExcelCmd;
        /// <summary>
        /// 导出Excel
        /// </summary>
        public DelegateCommand ExportExcelCmd => exportExcelCmd ?? (exportExcelCmd = new DelegateCommand(() =>
        {
            if (QueryMsg.Count == 0)
            {
                MessageBox.Show("请先查询后，再导出");
                return;
            }
            //先获取需要导出的路径
            SaveFileDialog sF = new SaveFileDialog();
            DateTime dt = DateTime.Now;
            string sj = dt.ToString("yyyyMMddHHmmss");
            sF.Filter = "(*.xlsx)|*.xlsx";
            sF.FilterIndex = 0;
            sF.RestoreDirectory = true;
            sF.CreatePrompt = true;
            sF.Title = "导出Excel文件到";
            sF.FileName = sj;
            sF.ShowDialog();
            string fileName = sF.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                MiniExcel.SaveAs(fileName, QueryMsg);
            }
        }));

        private DelegateCommand clearCmd;
        /// <summary>
        /// 清除记录
        /// </summary>
        public DelegateCommand ClearCmd => clearCmd ?? (clearCmd = new DelegateCommand(() =>
        {
            QueryMsg.Clear();
        }));

    }
}
